package cn.com.lms.business.questionnaire.web;

import cn.com.lms.business.core.util.SystemUserUtil;
import cn.com.lms.business.questionnaire.persistence.entity.UserQuestionnaireTitle;
import cn.com.lms.business.questionnaire.service.UserQuestionnaireTitleService;
import cn.com.lms.business.user.persistence.entity.SysUserVo;
import com.bnzj.core.persistence.query.CompareExpression;
import com.bnzj.core.persistence.query.CompareType;
import com.bnzj.core.persistence.query.IExpression;

import com.bnzj.core.persistence.query.QueryHelpBean;
import com.bnzj.core.rest.ResponseResult;
import com.bnzj.core.webmvc.util.WebMvcUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 16266
 * @DateTime: 2020/4/2 17:31
 * @Description: 用户问卷题目查询
 */
@Tag(name = "App问卷列表", description = "App问卷列表查询")
@RestController
@RequestMapping(value = "/questionnaire/user")
public class UserQuestionnaireTitleController {

    @Autowired
    private UserQuestionnaireTitleService userQuestionnaireTitleService;

    @Operation(summary = "分页查APP询问卷题目", description = "", parameters = {
            @Parameter(name = "offset", description = "起始页", schema = @Schema(name = "offset", description = "", implementation = String.class)),
            @Parameter(name = "limit", description = "页大小", schema = @Schema(name = "limit", description = "", implementation = String.class))
    })
    @GetMapping(value = "/title")
    public ResponseResult<Page<UserQuestionnaireTitle>> getQuestionnaireInfo(HttpServletRequest request) {
        Pageable pageable = WebMvcUtils.initPage(request, Sort.by(Sort.Direction.DESC, "create_time"));
        QueryHelpBean dicQueryBean = UserQuestionnaireTitleService.getQueryHelpBean();
        dicQueryBean.setPageable(pageable);
        SysUserVo currentUser = SystemUserUtil.getCurrentUser();
        Long userId = currentUser.getId();
        IExpression orCondition = CompareExpression.build("d.user_id", CompareType.equal, userId);
        dicQueryBean.addWhere(orCondition);
        Page<UserQuestionnaireTitle> page = userQuestionnaireTitleService.findAll(dicQueryBean);
        return ResponseResult.success(page);
    }
}
